package com.example.dmc_android_sellbreakfast_ver1;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

public class Adapter extends BaseAdapter {
	Context mContext;
	ArrayList<foods> mArrayList;

	public Adapter(Context mContext, ArrayList<foods> mArrayList) {
		super();
		this.mContext = mContext;
		this.mArrayList = mArrayList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return mArrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final foods item = mArrayList.get(position);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.layout_item_list, null);
		}
			TextView tv_name = (TextView)convertView.findViewById(R.id.tv_name);
			TextView tv_cost = (TextView)convertView.findViewById(R.id.tv_cost);
			ImageView image = (ImageView)convertView.findViewById(R.id.img_food);
			Spinner spn_cout = (Spinner) convertView.findViewById(R.id.spn_count);
			
			spn_cout.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					item.setCout(String.valueOf(arg2));
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
					
				}
				
			});
			tv_name.setText(item.getName());
			tv_cost.setText(item.getCost());
			image.setImageResource(item.getmImage());
			
		return convertView;
	}
}
